package com.code.generate;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TableManager {
    private JTable jTable;

    public TableManager(JTable jTable) {
        this.jTable = jTable;
    }

    public void clear() {
        clear(0);
    }

    public void clear(int start) {
        DefaultTableModel dtm = (DefaultTableModel) this.jTable.getModel();
        int rowCount = dtm.getRowCount();
        for (int i = rowCount - 1; i >= start; i--) {
            dtm.removeRow(i);
        }
        fixTableColumnWidth();
    }

    public void addRow(Object[] items) {
        DefaultTableModel dtm = (DefaultTableModel) this.jTable.getModel();
        int rowCount = dtm.getRowCount();
        items[0] = Integer.valueOf(rowCount + 1);
        dtm.addRow(items);
        fixTableColumnWidth();
    }

    public void updateRow(Object[] items, int row) {
        DefaultTableModel dtm = (DefaultTableModel) this.jTable.getModel();
        for (int j = 0; j < items.length; j++) {
            dtm.setValueAt(items[j], row, j);
        }
        fixTableColumnWidth();
    }

    public void moveUp() {
        DefaultTableModel dtm = (DefaultTableModel) this.jTable.getModel();
        int[] indexs = this.jTable.getSelectedRows();
        for (int index : indexs) {
            index = this.jTable.convertRowIndexToModel(index);
            if (index >= 1) dtm.moveRow(index, index, index - 1);
        }
    }

    public void moveDown() {
        DefaultTableModel dtm = (DefaultTableModel) this.jTable.getModel();
        int rowCount = dtm.getRowCount();
        int[] indexs = this.jTable.getSelectedRows();
        for (int i = indexs.length - 1; i >= 0; i--) {
            int index = this.jTable.convertRowIndexToModel(indexs[i]);
            if (index < rowCount - 1) dtm.moveRow(index, index, index + 1);
            dtm.moveRow(index, index, index - 1);
        }
    }

    public void fixTableColumnWidth() {
        for (int i = 0; i < this.jTable.getColumnCount(); i++)
            adjustColumnPreferredWidths(i);
    }

    private void adjustColumnPreferredWidths(int col) {
        TableColumnModel columnModel = this.jTable.getColumnModel();
        int maxwidth = 0;
        for (int row = 0; row < this.jTable.getRowCount(); row++) {
            TableCellRenderer rend = this.jTable.getCellRenderer(row, col);
            Object value = this.jTable.getValueAt(row, col);
            Component comp = rend.getTableCellRendererComponent(this.jTable, value, false, false, row, col);
            maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
        }
        TableColumn column = columnModel.getColumn(col);
        maxwidth = Math.max(getColumnHeaderWidth(column), maxwidth);
        column.setPreferredWidth(maxwidth + 4);
    }

    private int getColumnHeaderWidth(TableColumn col) {
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = new DefaultTableCellRenderer();
            ((DefaultTableCellRenderer) renderer).setText(col.getHeaderValue().toString());
        }
        Component comp = renderer.getTableCellRendererComponent(this.jTable, col.getHeaderValue(), false, false, 0, 0);
        return comp.getPreferredSize().width + 14;
    }

    public void deleteSelected() {
        DefaultTableModel model = (DefaultTableModel) jTable.getModel();
        int[] index = jTable.getSelectedRows();
        for (int i = index.length - 1; i >= 0; i--) {
            model.removeRow(index[i]);
        }
    }

    public void addEditor(int index, DefaultCellEditor editor) {
        DefaultTableColumnModel dcm = (DefaultTableColumnModel) jTable.getColumnModel();
        TableColumn column = dcm.getColumn(index);
        column.setCellEditor(editor);
    }
}